package com.fjp.heartguard.ui.report

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.app.data.bean.OlderInfo
import com.fjp.heartguard.app.eventViewModel
import com.fjp.heartguard.app.ext.init
import com.fjp.heartguard.app.ext.showToast
import com.fjp.heartguard.app.util.CacheUtil
import com.fjp.heartguard.databinding.FragmentReportLocalBinding
import com.fjp.heartguard.ui.report.adapter.ReportLocalAdapter
import com.fjp.heartguard.viewmodel.ReportLocalViewModel
import me.hgj.jetpackmvvm.base.appContext

class ReportLocalFragment : BaseFragment<ReportLocalViewModel, FragmentReportLocalBinding>() {
    var curIndex =1//正在上传的第几个报告
    var hasMore = false
    val data :ArrayList<OlderInfo> = arrayListOf()
    private var olderINfoList: ArrayList<OlderInfo>? = null
    private val reportAdapter = ReportLocalAdapter(data) { it, index ->
        val intent = Intent(context, ReportDetailActivity::class.java)
        intent.putExtra(Constants.Intent_OLDER, olderINfoList!![index])
        startActivity(intent)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = ClickProxy()
        olderINfoList = CacheUtil.getOlderInfoList()
        olderINfoList?.let {
            data.addAll(it.reversed())
            reportAdapter.notifyDataSetChanged()
        }
        if(data.size!=0){
            mDatabind.showPushBtn.visibility = View.VISIBLE
            mDatabind.emptyLl.visibility = View.GONE
        }else{
            mDatabind.showPushBtn.visibility = View.GONE
            mDatabind.emptyLl.visibility = View.VISIBLE
        }
        mDatabind.recyclerView.init(LinearLayoutManager(context, RecyclerView.VERTICAL, false), reportAdapter)
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.updateSuccess.observe(this){
            if(reportAdapter.data.size>0){
                reportAdapter.data.removeLast()
                CacheUtil.removeFirstOlderInfo()
            }
            appContext.showToast("上传成功")
            dismissLoading()
            if(reportAdapter.data.size==0){
                mDatabind.showPushBtn.visibility = View.GONE
                mDatabind.emptyLl.visibility = View.VISIBLE
                eventViewModel.updateLocalAllReport.postValue(true)
            }else{
                mDatabind.click?.push()
            }
            eventViewModel.updateLocalReport.postValue(true)

            reportAdapter.notifyDataSetChanged()
        }
        mViewModel.updateFailed.observe(this){
            appContext.showToast("上传失败")
        }
        eventViewModel.localReport.observeInFragment(this) {
            olderINfoList = CacheUtil.getOlderInfoList()
            olderINfoList?.let {
                data.clear()
                data.addAll(it.reversed())
                reportAdapter.notifyDataSetChanged()
                if(reportAdapter.data.size!=0){
                    mDatabind.showPushBtn.visibility = View.VISIBLE
                    mDatabind.emptyLl.visibility = View.GONE
                }else{
                    mDatabind.showPushBtn.visibility = View.GONE
                    mDatabind.emptyLl.visibility = View.VISIBLE
                }
            }
        }

    }


    inner class ClickProxy {

        fun push() {
            showLoading("正在上传报告...")
            mViewModel.update(reportAdapter.data.get(reportAdapter.data.size-1))
        }

    }

}